<?php
class TestComments extends CActiveRecord {
	public $verifyCode;
	
	public static function model($className = __CLASS__) {
		return parent::model($className);
	}

	public function tableName() {
		return strtolower('PsyTestComments');
	}
	
	public function rules() {
		return array(
            array('commentContent, userId, topicId', 'required'),
			array('verifyCode', 'captcha', 'on' => 'insert', 'message' => '您填写的验证码有误。')
		);
	}

    public function defaultScope() {
        return array(
            'order' => 'psyTestCommentId ASC'
        );
    }
    public function attributeLabels() {
        return array(
            'commentContent' => '评论内容'
        );
    }
    
    public function afterSave() {
    	if($this->isNewRecord) {
    		$this->topic->commentCount = TestComments::model()->count('topicId = '.$this->topicId);
    		$this->topic->save();
    	}
    }
    
    public function afterDelete() {
        $this->topic->commentCount = TestComments::model()->count('topicId = '.$this->topicId);
    	$this->topic->save();
    }

    public function relations() {
        return array(
            'topic' => array(self::BELONGS_TO, 'TestTopics', 'topicId',
                'joinType'=> 'RIGHT JOIN'
            ),
            'author' => array(self::BELONGS_TO, 'ForumMembers', 'userId',
				'joinType' => 'INNER JOIN'
			)
        );
    }
}
